layui.use(['table', 'ax','admin','form','excel','selectM'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var excel = layui.excel;
    var selectM = layui.selectM;
    /**
     * 基础字典管理
     */
    var Completion = {
        tableId: "completionTable",
        condition:{
            rankType:2
        }
    };
    //先隐藏市选择
    $(".city").hide();

    //查询市
    var pAjax = new $ax(Feng.ctxPath + "/region/selectChildren", function (data) {
        $("#city").empty();
        $("#city").append('<option value="">请选择市</option>')
        $("#city").append(data)
    })
    pAjax.set("regionLevel",2)
    pAjax.set("parentId",16)
    pAjax.start();
    form.render('select');

    //月份多选
    var monthSelect = selectM({
        //元素容器【必填】
        elem: '#month'
        //候选数据【必填】
        ,data: [{"value":1,"name":"1月"},{"value":2,"name":"2月"},{"value":3,"name":"3月"},{"value":4,"name":"4月"},{"value":5,"name":"5月"},{"value":6,"name":"6月"},{"value":7,"name":"7月"},{"value":8,"name":"8月"},{"value":9,"name":"9月"},{"value":10,"name":"10月"},{"value":11,"name":"11月"},{"value":12,"name":"12月"},]
        ,max:12
        ,width:300
        //候选项数据的键名
        ,field: {idName:'value',titleName:'name'}
        //添加验证
        // ,verify:'required'
        ,tips:"请选择月份,默认为全年"
    });
    /**
     * 初始化表格的列
     */
    Completion.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {field: 'rank', sort: false, title: '名次'},
            // {field: 'parentName', sort: false, title: '上级单位'},
            {field: 'region_name', sort: false, title: '单位名称'},
            {field: 'percent', sort: false, title: '完成率(%)'},
            // {field: 'base_score', sort: false, title: '基础分得分'},
            {field: 'data_count', sort: false, title: '任务数量'},
            {field: 'completion_count', sort: false, title: '完成数量'}
        ]];
    };



    /**
     * 渲染表格
     */
        // 渲染表格
    var tableResult = table.render({
            elem: '#' + Completion.tableId,
            url: Feng.ctxPath + '/chart/completionList',
            page: true,
            toolbar: true,
            height: "full-98",
            cellMinWidth: 100,
            cols: Completion.initColumn()
        });

    //搜索
    $('#btnSearch').click(function () {
        Completion.search()
    })

    //查询
    Completion.search = function () {
        var queryData = {};
        queryData['year'] = $("#year").val();
        queryData['month'] = monthSelect.values.toString();
        queryData['rankType'] = Completion.condition.rankType;
        queryData['city'] = $("#city").val();
        table.reload(Completion.tableId, {where: queryData});
    }


    form.on('radio(rankType)',function (data) {
        Completion.condition.rankType = data.value;
        if(data.value==2){
            $(".city").hide();
        }
        if(data.value==3){
            $(".city").show();
        }
        Completion.search();
    })


    //导出Excel表格
    $('#exportExcel').on('click', function(){
        var ajax = new $ax(Feng.ctxPath + "/chart/exportCompletionExcel", function (data) {
            // 重点！！！如果后端给的数据顺序和映射关系不对，请执行梳理函数后导出
            data = excel.filterExportData(data, [
                'parentName'
                ,'region_name'
                ,'data_count'
                ,'completion_count'
                ,'percent'
                ,'rank'
            ]);
            // 重点2！！！一般都需要加一个表头，表头的键名顺序需要与最终导出的数据一致
            data.unshift({ parentName: "上级单位", region_name: "单位名称",data_count: "任务数量",completion_count: '完成数量', percent: "完成率(%)",rank: "排名"});

            // 2. 调用设置样式的函数，传入设置的范围，支持回调
            excel.setExportCellStyle(data, 'A1:L1', {
                s: {
                    font: { bold:true, fgColor: { rgb: "FF0000" } },
                    alignment: {
                        horizontal: 'center',
                        vertical: 'center'
                    }
                }
            }, function(cell, newCell, row, config, currentRow, currentCol, fieldKey) {
                // 回调参数，cell:原有数据，newCell:根据批量设置规则自动生成的样式，row:所在行数据，config:传入的配置,currentRow:当前行索引,currentCol:当前列索引，fieldKey:当前字段索引
                return (currentRow === 0) ? newCell : cell;// 第一行表头加粗
            } );

            //设置表格宽度
            var colConf = excel.makeColConfig({
                'A': 120,
                'B': 80,
                'C': 100,
                'D': 150,
                'E': 120,
            }, 80);
            //var date = new Date();
            excel.exportExcel(data,'完成率排名.xlsx', 'xlsx', {
                extend: {
                    '!cols': colConf
                }
            });
        }, function (data) {
            Feng.error("导出Excel失败！" + data.responseJSON.message + "!");
        });
        ajax.set("year",$("#year").val());
        ajax.set("rankType",Completion.condition.rankType);
        ajax.set("city", $("#city").val());
        ajax.start();
    });

});
